package edu.hubu.talentmanagementsystem.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author moonlan
 * date 2021/3/9 下午3:48
 */
@SuppressWarnings(value = "unused")
@Aspect
@Component
public class ServiceLogAspect {

    @Pointcut("execution(* edu.hubu.talentmanagementsystem.service.impl.*.*(..))")
    public void pc() { }

    @Around("pc()")
    public Object around(ProceedingJoinPoint pjp) {
        Class<?> targetClass = pjp.getTarget().getClass();
        Logger logger = LoggerFactory.getLogger(targetClass);
        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        Method method = methodSignature.getMethod();
        Object o = null;
        try {
            logger.debug("正在执行 {}, {} 参数为 --> {}", method.getName(), method.getName(), Arrays.toString(pjp.getArgs()));
            o = pjp.proceed();
            return o;
        } catch (Throwable throwable) {
            logger.error("{} 的执行出错 --> {}", method.getName(), throwable.getMessage());
            throw new RuntimeException(throwable);
        } finally {
            logger.debug("{} 执行结果 --> {}", method.getName(), o);
        }
    }

}
